﻿using Candy.Infrastructure;
using Candy.Model;
using Candy.Web.Data;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace Candy.ViewModel
{
    public class SendMembershipRequestViewModel : ViewModelBase
    {

        private bool _isBusy;
        public bool IsBusy
        {
            get
            {
                return _isBusy;
            }
            set
            {
                _isBusy = value;
                RaisePropertyChanged("IsBusy");
            }
        }

        private ProjectDto _project;
        public ProjectDto Project
        {
            get
            {
                return _project;
            }
            set
            {
                _project = value;
                RaisePropertyChanged("Project");
            }
        }

        private bool _asGuest;
        public bool AsGuest
        {
            get
            {
                return _asGuest;
            }
            set
            {
                _asGuest = value;
                RaisePropertyChanged("AsGuest");
            }
        }

        private bool _asMember = true;
        public bool AsMember
        {
            get
            {
                return _asMember;
            }
            set
            {
                _asMember = value;
                RaisePropertyChanged("AsMember");
            }
        }

        private string _message;
        public string Message
        {
            get
            {
                return _message;
            }
            set
            {
                _message = value;
                RaisePropertyChanged("Message");
            }
        }

        INavigationService _navService;
        IDataService _dataService;
        public SendMembershipRequestViewModel(IDataService dataService, INavigationService navService, ProjectDto project)
        {
            _dataService = dataService;
            _navService = navService;
            Project = project;
        }

        private RelayCommand _send;
        public RelayCommand Send
        {
            get
            {
                return _send ?? (_send = new RelayCommand(ExecSend, CanSend));
            }
        }

        /// <summary>
        /// Checks whether the Send command is executable
        /// </summary>
        private bool CanSend()
        {
            return true;
        }

        /// <summary>
        /// Executes the Send command 
        /// </summary>
        private void ExecSend()
        {
            IsBusy = true;
            RoleType rt = AsGuest ? RoleType.GUEST : RoleType.MEMBER;

            _dataService.DataContext.RequestMembership(Project.Id, rt, Message, op =>
                {
                    IsBusy = false;
                    _navService.CloseMembershipRequestWindow();
                    if (op.HasError)
                    {
                        op.MarkErrorAsHandled();
                        MessengerInstance.Send<ErrorMessage>(new ErrorMessage(op.Error));
                    }
                    else
                    {
                       
                    }
                }, null
                );
        }




    }
}
